local skynet = require "skynet"
local tabletool = require "tabletool"
local timetool = require "timetool"
local base = require "base"
local filelog = require "filelog"

require "yydbenum"

local server = nil
local tablerobothelper = {}

function tablerobothelper.init(service)
	if server == nil then server = service end
end

function tablerobothelper.check_robot_numer()
	local table_data = server.table_data
	local robot_runstate = skynet.call(".robotmgr", "lua", "get_robot_runstate")
	local get_table_robotnum = skynet.call(".robotmgr", "lua", "get_table_robot_num", table_data.conf.game_type)
	local in_table_robotnum = 0
	local in_table_robot_rids = {}
	for index, seat in ipairs(table_data.seats) do
		if seat.rid ~= 0 and seat.playerinfo.level == 99 then
			in_table_robotnum = in_table_robotnum + 1
			table.insert(in_table_robot_rids, seat.rid)
		end
	end
	---filelog.sys_error("-----------tablerobothelper.robot_runstate----", robot_runstate, get_table_robotnum, in_table_robotnum)
	if not robot_runstate then
		skynet.send(".robotmgr", "lua", "remove_robots", in_table_robot_rids, in_table_robotnum)
		return 
	end
	if in_table_robotnum == get_table_robotnum then
		---filelog.sys_error("------------robot number is ok-----------")
	else
		if in_table_robotnum > get_table_robotnum then
			local remove_rids = nil
			local leave_robotnum = in_table_robotnum - get_table_robotnum
			if level_robotnum > #in_table_robot_rids then
				level_robotnum = #in_table_robot_rids 
				remove_robots = tabletool.deepcopy(in_table_robot_rids)
			else
				local temp_level_robotnum = leave_robotnum
				remove_rids = {}
				for index, value in ipairs(in_table_robot_rids) do
					if temp_level_robotnum > 0 then
						table.insert(remove_rids, value)
						temp_level_robotnum = temp_level_robotnum - 1
					end
				end
			end
			skynet.send(".robotmgr", "lua", "remove_robots", remove_rids, leave_robotnum)
		elseif in_table_robotnum < get_table_robotnum then
			local enter_robotnum = get_table_robotnum - in_table_robotnum
			---filelog.sys_error("--------------enter_robotnum----------", enter_robotnum)
			skynet.send(".robotmgr", "lua", "add_robot_to_table", skynet.self(), enter_robotnum, false)
		end
	end
end

function tablerobothelper.check_betinfo_for_robot()
	local table_data = server.table_data
	local robot_runstate = skynet.call(".robotmgr", "lua", "get_robot_runstate")
	if not robot_runstate then return end

	local tablelogic = server:get_logicbyname("tablelogic")
	--设置机器人下注
    local base_score_list = {}
    if table_data.conf.game_type == YYDBGameType.GAME_TYPE_QIANZHONG then
    	base_score_list = tabletool.deepcopy(table_data.conf.base_score_list)
    elseif table_data.conf.game_type == YYDBGameType.GAME_TYPE_MANREN then
    	---获取下注人数
    	local beted_num = tablelogic.get_allbetplayer_num(table_data)
    	if beted_num >= table_data.conf.max_bet_player_num then return end
    	for index, value in ipairs(table_data.conf.base_score_list) do
    		if value >= table_data.conf.min_bet_num then
    			table.insert(base_score_list, value)
    		end
    	end
    end
    
    local bet_btn_list = tabletool.deepcopy(table_data.conf.bet_btn_list)
    local bet_time = table_data.do_bet_timeout
    local robot_list = {}
    for _,seat in pairs(table_data.seats) do
        if seat.state == YYDBSeatState.SEAT_STATE_WAIT_BET then
            if seat.is_robot then
                if seat.cur_bet_cnt < table_data.conf.each_bet_times then
                	if table_data.conf.game_type == YYDBGameType.GAME_TYPE_QIANZHONG then
                    	table.insert(robot_list,seat.rid)
                    elseif table_data.conf.game_type == YYDBGameType.GAME_TYPE_MANREN then
                    	if #robot_list < 2 then
                    		table.insert(robot_list, seat.rid)
                    	end
                    end
                end
            end
        end
    end

    -- --通知机器人下注
    local sp_rids = { }
    if table_data.special_seats_rids then
        for _,cur_rid in pairs(table_data.special_seats_rids) do
           sp_rids[cur_rid] = true
        end
    end
    --filelog.sys_error("--------tablerobothelper.check_betinfo_for_robot------", robot_list, bet_btn_list, base_score_list, bet_time, sp_rids)
    skynet.send(".robotmgr", "lua", "robot_do_bet", robot_list, bet_btn_list, base_score_list, bet_time, sp_rids, table_data.conf.game_type) 
end

function tablerobothelper.check_robot_money()
	--local table_data = server.table_data

end

function tablerobothelper.check_robot_leave()
	local table_data = server.table_data
	local all_robot_rids = {}
	for index, seat in ipairs(table_data.seats) do
		if seat.rid ~= 0 and seat.playerinfo.level == 99 then
			table.insert(all_robot_rids, seat.rid)
		end
	end
	if #all_robot_rids > 0 then
		skynet.send(".robotmgr", "lua", "check_robot_leave", all_robot_rids)
	end
end

return tablerobothelper